﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jalal.Library.Utility
{
    public class Validation
    {
        private ValidationLevel level = ValidationLevel.Medium;

        public Validation(ValidationLevel level)
        {
            level = this.level;
        }

        public bool ValidateUsername(string username, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsUserName(username))
                {
                    errorMessage = RegExUtil.UserNameFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsUserName(username))
                {
                    errorMessage = RegExUtil.UserNameFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidateUsernameClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUsername_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsUserName(e.Value); sender.errormessage = '")
                .Append(RegExUtil.UserNameFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.UserNameFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUsername_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsUserName(e.Value); sender.errormessage = '")
                .Append(RegExUtil.UserNameFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.UserNameFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidatePassword(string password, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsPassword(password))
                {
                    errorMessage = RegExUtil.PasswordFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsPassword(password))
                {
                    errorMessage = RegExUtil.PasswordFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidatePasswordClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidatePassword_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsPassword(e.Value); sender.errormessage = '")
                .Append(RegExUtil.PasswordFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.PasswordFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidatePassword_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsPassword(e.Value); sender.errormessage = '")
                .Append(RegExUtil.PasswordFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.PasswordFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidateEmail(string email, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsEmail(email))
                {
                    errorMessage = RegExUtil.EmailFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsEmail(email))
                {
                    errorMessage = RegExUtil.EmailFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidateEmailClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateEmail_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsEmail(e.Value); sender.errormessage = '")
                .Append(RegExUtil.EmailFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.EmailFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateEmail_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsEmail(e.Value); sender.errormessage = '")
                .Append(RegExUtil.EmailFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.EmailFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidateUrl(string url, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsUrl(url))
                {
                    errorMessage = RegExUtil.UrlFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsUrl(url))
                {
                    errorMessage = RegExUtil.UrlFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidateUrlClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUrl_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsUrl(e.Value); sender.errormessage = '")
                .Append(RegExUtil.UrlFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.UrlFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUrl_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsUrl(e.Value); sender.errormessage = '")
                .Append(RegExUtil.UrlFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.UrlFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidateAddress(string address, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsAddress(address))
                {
                    errorMessage = RegExUtil.AddressFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsAddress(address))
                {
                    errorMessage = RegExUtil.AddressFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidateAddressClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUrl_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsAddress(e.Value); sender.errormessage = '")
                .Append(RegExUtil.AddressFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.AddressFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateUrl_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsAddress(e.Value); sender.errormessage = '")
                .Append(RegExUtil.AddressFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.AddressFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidatePainText(string painText, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsPlainText(painText))
                {
                    errorMessage = RegExUtil.TextOnlyFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsPlainText(painText))
                {
                    errorMessage = RegExUtil.TextOnlyFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidatePainTextClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidatePlainText_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsPlainText(e.Value); sender.errormessage = '")
                .Append(RegExUtil.TextOnlyFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.TextOnlyFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidatePlainText_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsPlainText(e.Value); sender.errormessage = '")
                .Append(RegExUtil.TextOnlyFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.TextOnlyFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }

        public bool ValidateDomainName(string domainName, out string errorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                if (!RegExUtil.IsDomainName(domainName))
                {
                    errorMessage = RegExUtil.DomainNameFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
            else
            {
                if (!RegExUtil.IsDomainName(domainName))
                {
                    errorMessage = RegExUtil.DomainNameFormatError;
                    return false;
                }
                else
                {
                    errorMessage = string.Empty;
                    return true;
                }
            }
        }

        public string GetValidateDomainNameClientScript(string controlClientId, bool setInlineErrorMessage)
        {
            if (this.level == ValidationLevel.Default)
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateDomainName_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsDomainName(e.Value); sender.errormessage = '")
                .Append(RegExUtil.DomainNameFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.DomainNameFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
            else
            {
                StringBuilder script = new StringBuilder("<script type='text/javascript'>function ValidateDomainName_")
                .Append(controlClientId).Append("(sender, e){").AppendLine()
                .Append(" e.IsValid = IsDomainName(e.Value); sender.errormessage = '")
                .Append(RegExUtil.DomainNameFormatError).Append("';").AppendLine()
                .Append(" SetErrorTheme('").Append(controlClientId).Append("', e.IsValid); ").AppendLine()
                .Append(setInlineErrorMessage ? "sender.innerHTML = '" + RegExUtil.DomainNameFormatError + "';" : string.Empty).AppendLine()
                .Append("}</script>");

                return script.ToString();
            }
        }
    }

    public enum ValidationLevel
    {
        Default = 0,
        Low = 1,
        Medium = 2,
        High = 3
    }
}
